class IDCard {

    //身份证
    IDCard:string;
    //身份id
    areaID:Object = {
        11: "北京",
        12: "天津",
        13: "河北",
        14: "山西",
        15: "内蒙古",
        21: "辽宁",
        22: "吉林",
        23: "黑龙江",
        31: "上海",
        32: "江苏",
        33: "浙江",
        34: "安徽",
        35: "福建",
        36: "江西",
        37: "山东",
        41: "河南",
        42: "湖北",
        43: "湖南",
        44: "广东",
        45: "广西",
        46: "海南",
        50: "重庆",
        51: "四川",
        52: "贵州",
        53: "云南",
        54: "西藏",
        61: "陕西",
        62: "甘肃",
        63: "青海",
        64: "宁夏",
        65: "新疆",
        71: "台湾",
        81: "香港",
        82: "澳门",
        91: "国外"
    }

    //性别
    sexMap:Object = {
        0: "女",
        1: "男"
    }
    info:object = {}
    status:object = {}

    // 构造方法
    constructor(IDCard:string):void{
        console.log('hi')
        this.IDCard = IDCard
    }

    /**
     * 身份证验证
     */
    check():object{
        let _this = this
        let str = [
            'getLenth',
            'validate',
            'getArea',
            'getBirthday',
            'getSex',
            'getAge'
        ]

        //循环遍历 验证
        str.forEach((v:string)=>{
            let rs = this[v]()
            if(rs.status == false){
                _this.status = rs
                return
            }
        })

        //判断是否有错误信息
        if( this.isEmpty(this.status)){
            this.status = {
                status: true,
                message: 'ok',
                data: this.info
            }
        }
        return this.status
    }

    /**
     * 获取出生地区
     */
    getArea():object{
        //过滤数据 x 替换成 a
        let IDCard:string = this.IDCard.replace(/x$/i, "a")
        // 地区列表 下标
        let index:string = IDCard.substr(0, 2);
        // 判断地区是否合法
        if(this.areaID[index] == undefined){
            console.log('你的身份证地区非法')
            return {
                status: false,
                message: '你的身份证地区非法!'
            };
        }
        this.info.area = this.areaID[index]
        return {
            status: true,
            message: this.areaID[index]
        }
    }

    /**
     * 获取出生年月日
     */
    getBirthday():object{
        //提取出生日期
        let sBirthday = this.IDCard.substr(6, 4) + "-" + Number(this.IDCard.substr(10, 2)) + "-" + Number(this.IDCard.substr(12, 2))
        let d = new Date(sBirthday.replace(/-/g, "/"))
        if(sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())){
            console.log('身份证上的出生日期非法')
            return {
                status: false,
                message: '身份证上的出生日期非法!'
            };
        }
        this.info.birthday = this.IDCard.substr(6, 4) + "-" + this.IDCard.substr(10, 2) + "-" + this.IDCard.substr(12, 2)
        return {
            status: true,
            message: this.IDCard.substr(6, 4) + "-" + this.IDCard.substr(10, 2) + "-" + this.IDCard.substr(12, 2)
        }
    }

    /**
     * 获取性别
     */
    getSex():object{
        //检测性别
        let sex:string
        if(this.IDCard.length == 15) {
            sex = this.sexMap[this.IDCard.substring(14, 15) % 2];
        }else if(this.IDCard.length == 18) {
            sex = this.sexMap[this.IDCard.substring(14, 17) % 2];
        }else{
            //不是15或者18,null
            return {
                status: false,
                message: '性别格式错误'
            };
        }
        this.info.sex = sex
        return {
            status: true,
            message: sex
        };
    }

    /**
     * 检测身份证长度
     */
    getLenth():object{
        //检测身份证长度
        if(!/^\d{17}(\d|x)$/i.test(this.IDCard)){
            console.log('你输入的身份证长度或格式错误')
            return {
                status: false,
                message: '你输入的身份证长度或格式错误!'
            }
        }
        return {
            status: true,
            message: ''
        }
    }

    /**
     * 检测身份证格式
     */
    validate():object{
        // 检测身份证 格式
        let iSum:number = 0
        for(var i = 17; i >= 0; i--){
            iSum += (Math.pow(2, i) % 11) * parseInt(this.IDCard.charAt(17 - i), 11);
        }
        if(iSum % 11 != 1){
            console.log('你输入的身份证号非法')
            return {
                status: false,
                message: '你输入的身份证号非法!'
            };
        }

        return {
            status: true,
            message: '校验成功！'
        };
    }

    /**
     * 获取年龄
     */
    getAge():object{
        let birthStr = this.IDCard.substr(6, 4) + "-" + this.IDCard.substr(10, 2) + "-" + this.IDCard.substr(12, 2)
        let r = birthStr.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/)
        if(r == null) return ''
        let d = new Date(r[1], r[3] - 1, r[4])
        let age = 0
        if(d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]) {
            let Y = new Date().getFullYear()
            age = (Y - r[1])
        }
        this.info.age = age
        return {
            status: true,
            message: age
        }
    }

    /*
    * 判断是否为空
    * obj 要判断的 变量 或者 对象 字符串
    * */
    isEmpty(obj:any):boolean{
        if(typeof obj == "undefined" || obj == null || obj == ""){
            return true;
        }else{
            return false;
        }
    }

}

/**
 * 演示数据
 */
let idCard:IDCard = new IDCard('371325199509093045');
let rs = idCard.check();

console.log(rs)